from rest_framework.generics import ListAPIView,RetrieveAPIView

from . import models, serializers

# OrderingFilter排序、SearchFilter搜索
from rest_framework.filters import OrderingFilter, SearchFilter
# 自定义过滤器类
from . import filters

# 分类筛选：django-filter：filter_backends配置DjangoFilterBackend，再在filter_fields中配置分组筛选的字段
from django_filters.rest_framework import DjangoFilterBackend

# 导入分页器组件
from . import paginations
# 查询免费课程
class FreeCourseListAPIView(ListAPIView):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by(
        "-orders").all()
    serializer_class = serializers.FreeCourseModelSerializer

    # 局部配置过滤器类
    # filter_backends = [OrderingFilter, SearchFilter, filters.LimitFilter]   # LimitFilter自定义过滤器
    filter_backends = [OrderingFilter, SearchFilter, DjangoFilterBackend]    # 配置DjangoFilterBackend
    # 参与排序的字段: ordering=-price,id,students
    ordering_fields = ['price', 'id', 'students']

    # 参与搜索的字段: search=python  (name字段中带python就ok)
    search_fields = ['name', 'brief']

    # 参与分类筛选的字段：
    filter_fields = ['course_category']

    # 使用基于django-filter自定义过滤器类
    filter_class = filters.CourseFilterSet

    # 使用分页器
    pagination_class = paginations.CoursePageNumberPagination
    # pagination_class = paginations.CourseLimitOffsetPagination
    # pagination_class = paginations.CourseCursorPagination

# 查询课程分类
class CategoryListAPIView(ListAPIView):
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('orders').all()
    serializer_class = serializers.CategoryModelSerializer

# 查询单个课程信息
class FreeCourseRetrieveAPIView(RetrieveAPIView):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by(
        "-orders").all()
    serializer_class = serializers.FreeCourseModelSerializer


# 查询课程章节
class ChapterListAPIView(ListAPIView):
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = serializers.CourseChapterModelSerializer


    # 分类筛选
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']
